package com.repair.controller;


import com.repair.entity.model.User;
import com.repair.service.FileService;
import com.repair.service.UserService;
import com.repair.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * <p>
 * 用户账户：用于保存用户登录信息 前端控制器
 * </p>
 *
 * @author tianwenle
 * @since 2023-04-12
 */
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @RequestMapping(value = "/upload")
    public R uploadFile(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {
        //获取上传文件名称
        String fileName = multipartFile.getOriginalFilename();
        //获取上传文件输入流，便于文件内容读写操作
        InputStream inputStream = multipartFile.getInputStream();

        // 文件的存储位置
        String location = "D:\\work\\gra\\server_zhang\\src\\main\\resources\\upload\\" + fileName;

        FileOutputStream writer = new FileOutputStream(location);

        byte[] bytes = new byte[1024];
        // 判断是否读取完成的额条件
        int size;
        while (-1 != (size = inputStream.read(bytes))) {
            writer.write(bytes, 0, size);
        }

        writer.close();
        inputStream.close();
        // 返回图片的地址
        return R.ok().data("location", "http://localhost:8080/upload/" + fileName);
    }

}

